//
//  LGNewNoteKlgView.m
//  LGAlertHUD
//
//  Created by 刘亚军 on 2020/4/9.
//

#import "LGNewNoteKlgView.h"
#import <Masonry/Masonry.h>
#import <YJExtensions/YJExtensions.h>
#import "LGNewNoteMarqueeLabel.h"
#import "LGNewNoteMarqueeButton.h"
#import <YJUtils/YJAudioPlayer.h>
#import <LGAlertHUD/LGAlertHUD.h>
#import "LGNewNoteManager.h"

@interface LGNewNoteKlgView ()<YJAudioPlayerDelegate>
@property (nonatomic,strong) UITextView *klgTextView;
@property (nonatomic,strong) LGNewNoteMarqueeButton *usBtn;
@property (nonatomic,strong) LGNewNoteMarqueeButton *unBtn;
@property (nonatomic,strong) YJAudioPlayer *audioPlayer;
@end
@implementation LGNewNoteKlgView
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self layoutUI];
    }
    return self;
}
- (void)layoutUI{
 
    [self addSubview:self.usBtn];
   [self.usBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.equalTo(self).offset(10);
       make.left.equalTo(self).offset(7);
       make.height.mas_equalTo(26);
       make.width.lessThanOrEqualTo(self).multipliedBy(0.42);
   }];
   
   [self addSubview:self.unBtn];
   [self.unBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       if (IsStrEmpty([LGNewNoteManager defaultManager].US_voice)) {
           make.left.equalTo(self).offset(7);
       }else{
           make.left.equalTo(self.usBtn.mas_right).offset(20);
       }
       make.bottom.height.equalTo(self.usBtn);
       make.width.lessThanOrEqualTo(self).multipliedBy(0.42);
   }];
    self.usBtn.hidden = IsStrEmpty([LGNewNoteManager defaultManager].US_voice);
    self.unBtn.hidden = IsStrEmpty([LGNewNoteManager defaultManager].UN_voice);
    
    [self addSubview:self.klgTextView];
    [self.klgTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (IsStrEmpty([LGNewNoteManager defaultManager].US_voice) && IsStrEmpty([LGNewNoteManager defaultManager].UN_voice)) {
            make.top.equalTo(self).offset(10);
        }else{
            make.top.equalTo(self.usBtn.mas_bottom).offset(10);
        }
        make.left.equalTo(self).offset(7);
        make.right.equalTo(self).offset(-10);
        make.bottom.equalTo(self).offset(-10);
    }];
    UIView *botLine = [UIView new];
    botLine.backgroundColor = [UIColor yj_colorWithRed:240 green:240 blue:240];
    [self addSubview:botLine];
    [botLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self);
        make.left.equalTo(self).offset(10);
        make.centerX.equalTo(self);
        make.height.mas_equalTo(0.8);
    }];
    self.klgTextView.hidden = !([LGNewNoteManager defaultManager].ExplainAttr && !IsStrEmpty([LGNewNoteManager defaultManager].ExplainAttr.string));
    if ([LGNewNoteManager defaultManager].ExplainAttr && !IsStrEmpty([LGNewNoteManager defaultManager].ExplainAttr.string)) {
        NSMutableAttributedString *attr = [LGNewNoteManager defaultManager].ExplainAttr.mutableCopy;
        [attr yj_setFont:16];
        [attr yj_setColor:[UIColor yj_colorWithRed:37 green:37 blue:37]];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.alignment = NSTextAlignmentLeft;
        [attr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attr.length)];
        self.klgTextView.attributedText = attr;
    }
    self.usBtn.titleLabel.text = [@"美 " stringByAppendingString:(IsStrEmpty([LGNewNoteManager defaultManager].US_phonetic) ? @"":[LGNewNoteManager defaultManager].US_phonetic)];
    self.unBtn.titleLabel.text = [@"英 " stringByAppendingString:(IsStrEmpty([LGNewNoteManager defaultManager].UN_phonetic) ? @"":[LGNewNoteManager defaultManager].UN_phonetic)];
}

- (CGFloat)actualHeight{
    
    CGFloat btnHeight = 0;
    if (!IsStrEmpty([LGNewNoteManager defaultManager].US_voice) || !IsStrEmpty([LGNewNoteManager defaultManager].UN_voice)) {
        btnHeight = 10 + 26;
    }
    
    CGFloat textHeight = 0;
    if ([LGNewNoteManager defaultManager].ExplainAttr && !IsStrEmpty([LGNewNoteManager defaultManager].ExplainAttr.string)) {
        textHeight = 20 + [self.klgTextView.attributedText boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width-30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height+6;
        if (textHeight > 88) {
            textHeight = 88;
        }
    }
    return btnHeight + textHeight;
}
- (void)stopPlayVoice{
    [self.audioPlayer stop];
    self.usBtn.selected = NO;
    self.unBtn.selected = NO;
}
- (void)invalidatePlayer{
    self.usBtn.selected = NO;
    self.unBtn.selected = NO;
    [self.audioPlayer invalidate];
}
- (void)unbtnClickAction{
    
    self.usBtn.selected = NO;
    [self.audioPlayer invalidate];
    self.audioPlayer.audioUrl = [LGNewNoteManager defaultManager].UN_voice;
    [self.audioPlayer play];
    self.unBtn.selected = YES;
}
- (void)usbtnClickAction{
    
    self.unBtn.selected = NO;
    [self.audioPlayer invalidate];
    self.audioPlayer.audioUrl =  [LGNewNoteManager defaultManager].US_voice;
    [self.audioPlayer play];
    self.usBtn.selected = YES;
}
#pragma mark - YJAudioPlayerDelegate
- (void)yj_audioPlayerDidPlayFailed{
    [LGAlert showErrorWithStatus:@"播放失败"];
    [self stopPlayVoice];
}
- (void)yj_audioPlayerDecodeError{
    [LGAlert showErrorWithStatus:@"播放失败"];
     [self stopPlayVoice];
}
- (void)yj_audioPlayerDidPlayComplete{
     [self stopPlayVoice];
}
- (void)yj_audioPlayerBeginInterruption{
     [self stopPlayVoice];
}

- (YJAudioPlayer *)audioPlayer{
    if (!_audioPlayer) {
        _audioPlayer = [[YJAudioPlayer alloc] init];
        _audioPlayer.delegate = self;
    }
    return _audioPlayer;
}
- (LGNewNoteMarqueeButton *)unBtn{
    if (!_unBtn) {
        _unBtn = [[LGNewNoteMarqueeButton alloc] initWithFrame:CGRectZero isLeftTitle:YES];
        _unBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        _unBtn.titleLabel.textColor = [UIColor yj_colorWithRed:152 green:152 blue:152];
        [_unBtn addTarget:self action:@selector(unbtnClickAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _unBtn;
}
- (LGNewNoteMarqueeButton *)usBtn{
    if (!_usBtn) {
        _usBtn = [[LGNewNoteMarqueeButton alloc] initWithFrame:CGRectZero isLeftTitle:YES];
        _usBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        _usBtn.titleLabel.textColor = [UIColor yj_colorWithRed:152 green:152 blue:152];
        [_usBtn addTarget:self action:@selector(usbtnClickAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _usBtn;
}
- (UITextView *)klgTextView{
    if (!_klgTextView) {
        _klgTextView = [[UITextView alloc] initWithFrame:CGRectZero];
        _klgTextView.editable = NO;
        _klgTextView.selectable = NO;
        _klgTextView.textContainerInset = UIEdgeInsetsZero;//上下间距为零
       
    }
    return _klgTextView;
}
@end
